package debby.com.lifecycle

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import debby.com.lifecycle.util.getRoundShape
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private lateinit var locationManager: LocationManager
    private lateinit var myViewModel: MyViewModel


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initData()
    }

    private fun initView() {
        btChange.background = getRoundShape(16f, ContextCompat.getColor(this, R.color.text_blue))
    }

    private fun initData() {
        //lifeCycles
        locationManager = LocationManager()
        lifecycle.addObserver(locationManager)
        myViewModel = ViewModelProviders.of(this).get(MyViewModel::class.java)
        //计时器
        myViewModel.getElapsedTime().observe(this, Observer<Long> {
            val hour = it / 3600
            val minute = it / 60 % 60
            val second = it % 60
            when {
                hour != 0L -> tvMessage.text = String.format("计时%d时%d分%d秒", hour, minute, second)
                minute != 0L -> tvMessage.text = String.format("计时%d分%d秒", minute, second)
                else -> tvMessage.text = String.format("计时%d秒", second)
            }
        })
        //改变计时器
        btChange.setOnClickListener {
            myViewModel.changeData()
        }
        //测试输入持久化，忽略
//        myViewModel.getInputData().observe(this, Observer {
//            if (it != null) {
//                when {
//                    it[1] != null -> etInput1.setText(it[1])
//                    it[2] != null -> etInput2.setText(it[2])
//                    it[3] != null -> etInput3.setText(it[3])
////                    it[4] != null -> etInput4.setText(it[4])
//                }
//            }
//        })
//
//        etInput1.addTextChangedListener(object : TextWatcher {
//            override fun afterTextChanged(s: Editable?) {
//            }
//
//            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//            }
//
//            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//                myViewModel.setInputData(1, s!!.toString())
//            }
//        })
//        etInput2.addTextChangedListener(object : TextWatcher {
//            override fun afterTextChanged(s: Editable?) {
//
//            }
//
//            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//            }
//
//            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//                myViewModel.setInputData(2, s!!.toString())
//            }
//        })
//
//        etInput3.addTextChangedListener(object : TextWatcher {
//            override fun afterTextChanged(s: Editable?) {
//
//            }
//
//            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//            }
//
//            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//                myViewModel.setInputData(3, s!!.toString())
//            }
//        })
//        etInput4.addTextChangedListener(object : TextWatcher {
//            override fun afterTextChanged(s: Editable?) {
//
//            }
//
//            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//            }
//
//            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//                myViewModel.setInputData(4, s!!.toString())
//            }
//        })

    }

}
